<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_SlideModel extends ADS_DefaultModel {

    public function countSlides() {
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
          %if', (bool)$this->useFilter, 'WHERE %and', $this->filter
        )->cnt;
    }
    
    public function getSlides() {
        return dibi::fetchAll('
          SELECT            
            t1.slideId,
            t1.name,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t2.post_title,
            t2.guid
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
           %if', (bool)$this->useFilter, 'WHERE %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset, '%end'
        );
    }
     
    public function getSlide($slideId) {
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            t1.slideId,
            t1.name,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t1.layoutId,
            t2.ID as postId,
            t2.post_title,
            t2.guid
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
          WHERE
            slideId = %i
        ', $slideId);
    }
    
    public function getSlideWithPosition($position) {
        if(!is_int($position)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            t1.slideId,
            t1.name,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t1.layoutId,
            t2.ID as postId,
            t2.post_title,
            t2.guid
          FROM
            [:wp:ads_schema] AS t1
          INNER JOIN
            [:wp:posts] AS t2 ON t1.layoutId = t2.ID
          WHERE
            position = %i
        ', $position);
    }
    
    public function getLayouts() {
        return dibi::fetchAll("
          SELECT
            t1.post_title,
            t1.ID
          FROM
            [:wp:posts] AS t1
          WHERE
            post_type = 'page'
          AND
            post_status = 'publish'
        ");
    }
    
    public function addSlide($slide){
        if(!is_array($slide)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $slide['position'] = ((int)dibi::fetch('SELECT MAX(position) as position FROM [:wp:ads_schema]')->position)+1;
        dibi::query('INSERT INTO [:wp:ads_schema]', $slide);
    }
    
    public function editSlide($slideId, $slide){
        if(!is_array($slide) || !is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_schema] SET', $slide, 'WHERE slideId = %i', $slideId);
    }
    
    public function removeSlide($slideId){
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $slide = $this->getSlide($slideId);
        //update positions
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE  
                position > %i
        ', $slide->position);
        dibi::query('DELETE FROM [:wp:ads_schema] WHERE slideId = %i LIMIT 1', $slideId);
    }
    
    public function increaseSlidePosition($slideId){        
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get slide
        $slide = dibi::fetch('SELECT * FROM [:wp:ads_schema] WHERE slideId = %i',$slideId);
        //update slide on next position
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE 
                position = %i + 1
        ', $slide->position);
        //update slide
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position + 1 
            WHERE 
                slideId = %i 
        ', $slideId);
    }
    
    public function decreaseSlidePosition($slideId){        
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get slide
        $slide = dibi::fetch('SELECT * FROM [:wp:ads_schema] WHERE slideId = %i',$slideId);
        //update slide on next position
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position + 1 
            WHERE 
                position = %i - 1
        ', $slide->position);
        //update slide
        dibi::query('
            UPDATE 
                [:wp:ads_schema] 
            SET 
                position = position - 1 
            WHERE 
                slideId = %i 
        ', $slideId);
    }

}
